package com.lj.api.v1.c;

import com.alibaba.fastjson.JSONObject;
import com.lj.api.aspect.tools.RedisTools;
import com.lj.api.common.CodeMsg;
import com.lj.api.common.CommUtils;
import com.lj.api.common.Result;
import com.lj.api.common.WebUtil;
import com.lj.api.conifg.RedisUtils;
import com.lj.api.conifg.SysSettings;
import com.lj.mcore.entity.*;
import com.lj.mcore.service.IGoodsClassService;
import com.lj.mcore.service.IGoodsTypeservice;
import com.lj.mcore.service.IStoreClassService;
import com.lj.mcore.service.IStoreService;
import com.lj.mcore.tools.StoreClassTools;
import com.lj.mcore.utils.PageBean;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/c")
public class GoodsClassController {

    @Autowired
    IGoodsClassService goodsClassService;

    @Autowired
    RedisTools redisTools;

    @Autowired
    IGoodsTypeservice goodsTypeservice;

    @Autowired
    IStoreClassService storeClassService;

    @Autowired
    IStoreService storeService;

    @RequestMapping("/goodsClassList")
    public String gcList(){
        List list1 = (List) redisTools.get(SysSettings.HOMEPAGE_NAVTREE);

        if(list1==null || list1.size()<=0 ) {
            list1 = new ArrayList();
            List<GoodsClass> list = goodsClassService.query(IGoodsClassService.HQL_LIST, null, -1, -1);
            for (GoodsClass goodsClass : list) {
                Map map = new HashMap();
                map.put("label", goodsClass.getClassName());
                map.put("value", goodsClass.getId());
                addChild(goodsClass, map);
                list1.add(map);
            }
            redisTools.set(SysSettings.HOMEPAGE_NAVTREE,list1);
        }
        System.out.println(JSONArray.fromObject(list1));
        return Result.ok(list1);
    }

    public Map addChild(GoodsClass goodsClass,Map map){
        if (goodsClass.getChilds().size()>0){
            List<GoodsClass> list = goodsClass.getChilds();
            List list1 = new ArrayList();
            for(GoodsClass goodsClass1:list){
                Map map1 = new HashMap();
                map1.put("label",goodsClass1.getClassName());
                map1.put("value",goodsClass1.getId());
                addChild(goodsClass1,map1);
                list1.add(map1);
            }
            map.put("children",list1);
        }
        return map;
    }

    @RequestMapping("/g_spec_info")
    public String gcList(String cid){
        GoodsClass goodsClass = goodsClassService.getObjById(Long.valueOf(cid));
        GoodsType goodsType = goodsClass.getGoodsType();
        List list = new ArrayList();
        if (goodsClass!=null){
            List<GoodsSpecification>  goodsTypeGss= goodsType.getGss();
            for(GoodsSpecification specification : goodsTypeGss){
                Map map = new HashMap();
                map.put("gs_id",specification.getId());
                map.put("name",specification.getName());
                List<GoodsSpecProperty> properties = specification.getProperties();
                if (properties.size()>0){
                    List pList = new ArrayList();
                    for (GoodsSpecProperty specProperty: properties){
                        Map map1 = new HashMap();
                        map1.put("gsp_id",specProperty.getId());
                        map1.put("value",specProperty.getValue());
                        pList.add(map1);
                    }
                    map.put("properties",pList);
                }

                list.add(map);
            }
        }
        return Result.ok(list);
    }


    @RequestMapping("/storeclass_tree")
    public String storeclass_tree(String sid){

        if (WebUtil.checkEmpty(sid)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        List<StoreClass> storeClasses =storeClassService.storeClassTree(Long.valueOf(sid));
        List list = new ArrayList();
        for (StoreClass storeClass:storeClasses){
            Map map = new HashMap();
            map.put("scid",storeClass.getId());
            map.put("className",storeClass.getClassName());
            if (storeClass.getChilds().size()>0){
                List list1 = StoreClassTools.ergodicChildren(storeClass.getChilds());
                if (list1.size()>0){
                    map.put("children",list1);
                }
            }
            list.add(map);
        }

        return Result.ok(list);
    }

    @RequestMapping("/storeclasslist")
    public String storeclasslist(String sid,String currentPage,String pageSize,String name){

        if (WebUtil.checkEmpty(sid,currentPage,pageSize)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }


        int page = CommUtils.null2Int(currentPage)<=0?1:CommUtils.null2Int(currentPage);
        int pagesize = CommUtils.null2Int(pageSize)<=0?1:CommUtils.null2Int(pageSize);
        pagesize = CommUtils.null2Int(pageSize)>200?200:CommUtils.null2Int(pageSize);
        Map query = new HashMap();
        query.put("sid",sid);
        if (!CommUtils.null2String(name).equals("")){
            query.put("name",name);
        }
        PageBean pagebean = storeClassService.getList(pagesize,page,query);
        List<StoreClass> list1 = pagebean.getList();
        List list = new ArrayList();
        for (StoreClass storeClass:list1){
            Map map = new HashMap();
            map.put("scid",storeClass.getId());
            map.put("className",storeClass.getClassName());
            map.put("level",storeClass.getLevel());
            map.put("display",storeClass.getDisplay());
            map.put("parentPath", StoreClassTools.parentPath(storeClass));
            map.put("sequence",storeClass.getSequence());
            list.add(map);
        }

        Map info = new HashMap();
        info.put("objs", JSONObject.toJSON(list));
        info.put("totalPage", pagebean.getTotalPage());
        info.put("pageSize", pagebean.getPageSize());
        info.put("rows", pagebean.getAllRow());
        info.put("currentPage", pagebean.getCurrentPage());
        return Result.ok(info);
    }

    @RequestMapping("/sclass_add")
    public String sclass_add(String sid,String pid,String className,String display,String sequence){
        if (WebUtil.checkEmpty(sid,className,display,sequence)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        StoreClass storeClass = new StoreClass();
        storeClass.setStore(storeService.getObjById(Long.valueOf(sid)));
        storeClass.setDisplay(Integer.valueOf(display));
        storeClass.setClassName(className);
        storeClass.setSequence(Integer.valueOf(sequence));
        if (pid!=null && pid.length()>0){
            StoreClass parent = storeClassService.getObjById(Long.valueOf(pid));
            storeClass.setParent(parent);
            storeClass.setLevel(parent.getLevel()+1);
        }else{
            storeClass.setLevel(0);
        }
        storeClassService.save(storeClass);
        return Result.ok();
    }

    @RequestMapping("/sclass_edit")
    public String sclass_edit(String id,String pid,String className,String display,String sequence){
        if (WebUtil.checkEmpty(id,className,display,sequence)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }
        StoreClass storeClass = storeClassService.getObjById(Long.valueOf(id));

        storeClass.setDisplay(Integer.valueOf(display));
        storeClass.setClassName(className);
        storeClass.setSequence(Integer.valueOf(sequence));
        if (pid!=null && pid.length()>0){
            StoreClass parent = storeClassService.getObjById(Long.valueOf(pid));
            storeClass.setParent(parent);
            storeClass.setLevel(parent.getLevel()+1);
        }else{
            storeClass.setLevel(0);
        }
        storeClassService.update(storeClass);
        return Result.ok();
    }

    @RequestMapping("/sclass_info")
    public String sclass_info(String id){
        if (WebUtil.checkEmpty(id)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        StoreClass storeClass = storeClassService.getObjById(Long.valueOf(id));
        if (storeClass==null || storeClass.isDeleteStatus()){
            return Result.fail(CodeMsg.ILLEGAL_PARAMS);
        }
        Map map = new HashMap();
        map.put("scid",storeClass.getId());
        map.put("className",storeClass.getClassName());
        map.put("level",storeClass.getLevel());
        map.put("display",storeClass.getDisplay());
        if (storeClass.getParent()!=null){
            map.put("pid",storeClass.getId());
            List list = StoreClassTools.ergodicParentIds(storeClass);
            if (list.size()>0){
                Collections.reverse(list);
                map.put("pid_array",list);
            }
        }
        map.put("parentPath", StoreClassTools.parentPath(storeClass));
        map.put("sequence",storeClass.getSequence());
        return Result.ok(map);
    }

    @RequestMapping("/sclass_del")
    public String sclass_del(String id){
        if (WebUtil.checkEmpty(id)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        StoreClass storeClass = storeClassService.getObjById(Long.valueOf(id));
        storeClass.setDeleteStatus(true);
        storeClassService.update(storeClass);
        return Result.ok();
    }


}
